/**
 * @description  : 证照-页签组件
 * @descriptionDetail : 证照-页签组件
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-12-05 10:36:34
 */
<template>
  <view class="page">
    <!-- <view class="retail">
      <fsection :title="title.basemsg" class="pl-16"></fsection>
      <u-gap :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
    </view> -->
    <view class="rdcontent">
      <u-form :model="licenceForm" :rules="rules" ref="uForm">
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              许可证件
            </view>
            <view class="msg-context">
              {{ basemsg.licNo || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              老许可证件
            </view>
            <view class="msg-context">
              {{ basemsg.oldLicNo || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <view class="msg-title">
              企业名称（字号）
            </view>
            <view class="msg-context">
              {{ basemsg.companyName || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <view class="msg-title">
              经营地址
            </view>
            <view class="msg-context">
              {{ basemsg.businessAddr || "/" }}
            </view>
          </u-col>
        </u-row>

        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="12">
            <view class="msg-title">
              许可有效期
            </view>
            <view class="msg-context">
              {{ basemsg.validateStart || "/" }}至 {{ basemsg.validateEnd || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              许可证状态
            </view>
            <view class="msg-context">
              {{ basemsg.licStatusName || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              系统编码
            </view>
            <view class="msg-context">
              {{ retailId || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              许可范围
            </view>
            <view class="msg-context">
              {{ basemsg.managerScopeName|| "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              管辖组织机构
            </view>
            <view class="msg-context">
              {{ basemsg.issueOrgName || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              制证日期
            </view>
            <view class="msg-context">
              {{ basemsg.issueDate || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              初制证日期
            </view>
            <view class="msg-context">
              {{ basemsg.firstAccrediDate || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              持证人
            </view>
            <view class="msg-context">
              {{ basemsg.managerName || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              企业类型
            </view>
            <view class="msg-context">
              {{ basemsg.ecoTypeName || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              供货单位
            </view>
            <view class="msg-context">
              {{ basemsg.supplyCompanyName || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              供货状态
            </view>
            <view class="msg-context">
              {{ basemsg.supplyStatusName || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              群体类型
            </view>
            <view class="msg-context">
              {{ specialTypeName }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              其他群体类型
            </view>
            <view class="msg-context">
              {{ basemsg.specialTypeOther || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              享受合理布局照顾政策
            </view>
            <view class="msg-context">
              {{ basemsg.layoutPolicyTypeName || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              零售业态
            </view>
            <view class="msg-context">
              {{ basemsg.bizFormatName || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">
              营业执照登记状态
            </view>
            <view class="msg-context">
              {{ basemsg.registeredStatusName || "/" }}
            </view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">
              失效日期
            </view>
            <view class="msg-context">
              {{ basemsg.invalidTime || "/" }}
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" :required="true" label="统一社会信用代码" prop="businessLicNo">
              <u-input placeholder="请输入" v-model="licenceForm.businessLicNo" />
            </u-form-item>

          </u-col>

        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item label="营业执照有效期类型" :required="true" :label-position="labelPosition" prop="businessValidType">
              <dic-input :keyWord="$u.dic.DIC_KEYS.L_BUSINESS_VALID_TYPE" :dicCode="licenceForm.businessValidType" inputAlign="left" type="select" :select-open="
              dicSelectOpen('businessValidType', $u.dic.DIC_KEYS.L_BUSINESS_VALID_TYPE)
            " placeholder="请选择" v-model="licenceForm.businessValidTypeName" @changeInputModelData="
              (name) => {
                $set(licenceForm, 'businessValidTypeName', name);
              }
            " @click="
              showDic(
                $u.dic.DIC_KEYS.L_BUSINESS_VALID_TYPE,
                licenceForm.businessValidType,
                'businessValidType'
              )
            " />
            </u-form-item>
          </u-col>

        </u-row>
        <u-row gutter="16" align="top" class="divided-line" v-if="licenceForm.businessValidType!=='01'">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" label="营业执照有效期起" right-icon="arrow-right" ref="endChangeRef">
              <u-input placeholder="营业执照有效期起" :disabled="true" input-align="left" v-model="licenceForm.businessValidStart" @click="clicDatekHandle('businessValidStart')" />
            </u-form-item>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line" v-if="licenceForm.businessValidType!=='01'">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" label="营业执照有效期止" right-icon="arrow-right" ref="endChangeRef">
              <u-input placeholder="营业执照有效期止" :disabled="true" input-align="left" v-model="licenceForm.businessValidEnd" @click="clicDatekHandle('businessValidEnd')" />
            </u-form-item>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item label="二维码制卡" :label-position="labelPosition">
              <dic-input :keyWord="$u.dic.DIC_KEYS.L_QR_CARD_STATUS" :dicCode="licenceForm.isQrCard" inputAlign="left" type="select" :select-open="
              dicSelectOpen('isQrCard', $u.dic.DIC_KEYS.L_QR_CARD_STATUS)
            " placeholder="请选择" v-model="licenceForm.isQrCardName" @changeInputModelData="
              (name) => {
                $set(licenceForm, 'isQrCardName', name);
              }
            " @click="
              showDic(
                $u.dic.DIC_KEYS.L_QR_CARD_STATUS,
                licenceForm.isQrCard,
                'isQrCard'
              )
            " />
            </u-form-item>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" label="NFC卡号">
              <u-input placeholder="请输入" v-model="licenceForm.nfcNo" />
            </u-form-item>
          </u-col>
        </u-row>
      </u-form>
      <view class="popup-submit-btns">
        <u-button class="u-btn" type="primary" @click="submitLngLat('01')">保存</u-button>
        <!-- <u-button class="u-btn" type="primary" @click="submitLngLat('02')">提交</u-button> -->
      </view>
    </view>
    <u-calendar :mode="mode" :max-date="yestDate" v-model="businessValidStart" @change="changeDate('businessValidStart',$event)">
    </u-calendar>
    <u-calendar :mode="mode" :max-date="yestDate" v-model="businessValidEnd" @change="changeDate('businessValidEnd',$event)">
    </u-calendar>
    <dic-choice :keyWord="dicKeyWords" :currentSelect="dicCurrentSelect" v-model="dicShow" @confirm="selectDicConfirm"></dic-choice>
  </view>
</template>

<script>
import { dateUtils } from "@/common/util";
import DicInput from "@/components/dic_input.vue";
import DicChoice from "@/components/dic_choice.vue";
import fsection from "@/components/form-section.vue";
import certificateService from "@/service/certificate/certificate.service.js";
export default {
  components: {
    fsection,
    DicChoice,
    DicInput
  },
  props: {
    licenceInfo: {
      type: Function,
      default: () => {}
    }
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  data() {
    return {
      mode: "date",
      yestDate: this.$u.timeFormat(Date.now()),
      title: {
        basemsg: "证件信息"
      },
      licenceForm: {
        // specialType: this.basemsg.specialType || '', // 群体类型
        // tmpSpecialType:'',
        // specialTypeOther:this.basemsg.specialTypeOther || '', // 其他群体类型
        // bizFormat:this.basemsg.bizFormat || '', // 零售业态
        // businessLicNo: this.basemsg.businessLicNo || '', // 统一社会信用代码
        // businessValidType: this.basemsg.businessValidType || '', // 营业执照有效期类型
        // businessValid: [this.basemsg.businessValidStart,this.basemsg.businessValidEnd,], // 营业执照有效期起止
        // registeredStatus:this.basemsg.registeredStatus || '', // 营业执照登记状态
        // isQrCard: '', // 二维码制卡
        // nfcNo: this.basemsg.nfcNo || '', // NFC卡号
      },
      dicShow: false,
      dicCurrentSelect: "",
      dicKeyWords: "",
      businessValidStart: false,
      businessValidEnd: false,
      rules: {
        businessLicNo: [
          {
            required: true,
            message: "请输入统一社会信用代码",
            trigger: ["change", "blur"]
          }
        ],
        businessValidType: [
          {
            required: true,
            message: "请选择营业执照有效期类型",
            trigger: ["change", "blur"]
          }
        ]
      },
      labelPosition: "top",
      basemsg: {},
      specialTypeName: "",
      retailId: ""
    };
  },
  created() {},
  mounted() {
    this.licenceInfo().then(res => {
      if (res.success) {
        this.basemsg = JSON.parse(JSON.stringify(res.bean.licenseInfoPre));
        this.retailId = this.basemsg.retailerUuid.substring(23, 32);
        this.getSpecialType();
        this.licenceForm = {
          specialType: this.basemsg.specialType || "", // 群体类型
          tmpSpecialType: "",
          specialTypeOther: this.basemsg.specialTypeOther || "", // 其他群体类型
          bizFormat: this.basemsg.bizFormat || '', // 零售业态
          businessLicNo: this.basemsg.businessLicNo||'', // 统一社会信用代码
          businessValidType: this.basemsg.businessValidType || '', // 营业执照有效期类型
          businessValid: [
            this.basemsg.businessValidStart,
            this.basemsg.businessValidEnd
          ], // 营业执照有效期起止
          businessValidStart:this.basemsg.businessValidStart,
          businessValidEnd:this.basemsg.businessValidEnd,
          registeredStatus: this.basemsg.registeredStatus || '', // 营业执照登记状态
          isQrCard: this.basemsg.isQrCard||"", // 二维码制卡
          isQrCardName: this.basemsg.isQrCard?'已制卡':'未制卡', // 二维码制卡
          nfcNo: this.basemsg.nfcNo || "" // NFC卡号
        };
        this.$refs.uForm.setRules(this.rules);
      }
    });
  },
  methods: {
    getSpecialType() {
      if (this.basemsg.specialType) {
        let tmp = [];
        this.specialType = this.basemsg.specialType.split(",");
        this.specialType.map(item => {
          tmp.push(
            this.$u.dic.getDicNameByKeyCode(this.$u.dic.DIC_KEYS.SPECIAL, item)
          );
        });
        if (tmp.length > 0) {
          this.specialTypeName = tmp.join(",");
        }
      } else {
        this.specialTypeName = "/";
      }
    },
    submitLngLat() {
      
      if (
        this.licenceForm.businessValidStart &&
        this.licenceForm.businessValidEnd
      ) {
        if (
          new Date(this.licenceForm.businessValidStart) >
          new Date(this.licenceForm.businessValidEnd)
        ) {
          return this.$u.toast(
            "营业执照有效期开始日期不能大于营业执照有效期结束日期"
          );
        }else{
          this.licenceForm.businessValid=[this.licenceForm.businessValidStart,this.licenceForm.businessValidEnd]
        }
      }
      this.$refs.uForm.validate(valid => {
        if (!valid) return
          certificateService
            .saveOrUpdateRetailList({
              ...this.licenceForm,
              retailerUuid: this.basemsg.retailerUuid
            }).then(res => {
              if (res.success) {
                // this.$u.success('采集成功', 200, () => {
                //     that.backPrev({
                //         paramKey: "params",
                //         param: {},
                //     });
                // });
                // this.$u.toast("保存成功");
                this.backPrev({
                    paramKey: "SaveRefresh",
                    param: {},
                });
              } else {
                this.$u.toast("保存失败");
              }
            })
            // .catch(e => {
            //   this.$u.toast(e);
            // });
        
      });
    },
    // 数据字典组件选择事件
    dicSelectOpen: function(keyWord) {
      if (this.dicKeyWords == keyWord && this.dicShow) {
        return true;
      }
      return false;
    },
    showDic: function(key, cur, fileName) {
      // if (!this.state.canEdit) return;
      if (this.dicShow) return;
      this.dicKeyWords = key;
      this.dicCurrentSelect = cur;
      this.dicShow = true;
    },
    selectDicConfirm: function(e) {
      // 类型
      if (this.dicKeyWords == this.$u.dic.DIC_KEYS.L_BUSINESS_VALID_TYPE) {
        this.licenceForm.businessValidTypeName = e[0].label;
        this.licenceForm.businessValidType = e[0].extra.bcbCode;
        if(this.licenceForm.businessValidType=='01'){
          this.licenceForm.businessValid=[]
        }
      }
      if (this.dicKeyWords == this.$u.dic.DIC_KEYS.L_QR_CARD_STATUS) {
        this.licenceForm.isQrCardName = e[0].label;
        this.licenceForm.isQrCard = e[0].extra.bcbCode;
      }
    },
    clicDatekHandle(type) {
      this[type] = true;
    },
    changeDate(type, e) {
      this.licenceForm[type] = dateUtils.dateFormat(
        "yyyy-MM-dd",
        new Date(e.result)
      );
    }
  }
};
</script>

<style lang="scss" scoped>
@import "./retailItem.scss";
.page {
  padding: 0;
  //   display: flex;
  //   justify-content: center;
}
.popup-submit-btns {
  width: calc(100% - 30px);
  position: fixed;
  bottom: 10rpx;
}
.rdcontent {
  padding-bottom: 100px;
}
</style>
